/**
 * 
 */
package myproject.main;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Test;

import myproject.api.ThreeD;
import myproject.mapper.ArrayThreeMapper;
import myproject.utils.SqlSessionFactoryUtils;

/**@version:
 * @Description: 按照1000个号码生成xls文件
 * @author ：luokai
 * @date: date{2021年11月11日21:42:17} 
 */
public class TestMain {
	private static final Logger log = Logger.getLogger(TestMain.class);
	private static final String filePath="E:/lottery/welfare/newwelfare.txt";
	private static final String outPath ="E:/lottery/welfare/sort/";
	
	public static void main(String[] args) {
//		countRemainder();
//		insertResult();
		
//		ArrayDatabase.assortNewTxt(outPath,"welfare");
//		ArrayDatabase.assortNewXls(outPath,"welfare");
		
	}
	/**
	 **  将1000个结果插入到表格1000中
	 **
	 **/
	@Test
	public static void insertResult(){
		SqlSession sqlSession = null;
		long startTime=System.currentTimeMillis();
		try {
			sqlSession = SqlSessionFactoryUtils.openSqlSession();
			ArrayThreeMapper arrayThreeMapper = sqlSession.getMapper(ArrayThreeMapper.class);
			
			//按和值分类开奖数据
			List<ThreeD> threelist= new ArrayList<ThreeD>();
			
			ArrayCount arrayCount=new ArrayCount();
			for(int i=0;i<=9;i++) {
				for(int j=0;j<=9;j++) {
					for(int k=0;k<=9;k++) {
						ThreeD threeD=new ThreeD();
						log.info(i+""+j+""+k);
						threeD.setHundreds(i);
						threeD.setTens(j);
						threeD.setOnes(k);
						threeD.setResult(i+""+j+""+k);
						threeD.setForm(	arrayCount.countForm(threeD));
						threeD.setSpan(arrayCount.countSpan(threeD));
						threeD.setSum(arrayCount.countSum(threeD));
						threeD.setPacket(arrayCount.countPacket(threeD));
						threeD.setRemainder(arrayCount.countRemainder(threeD));
						
						if(threeD.getForm().equals("组三")) {
							threeD.setArray(threeD.getHundreds() == threeD.getTens() ? threeD.getTens() : threeD.getOnes());
						}
						
						threeD.setSize(arrayCount.countSize(i) + arrayCount.countSize(j) + arrayCount.countSize(k));
						threeD.setOddeven(arrayCount.countOddEven(i) + arrayCount.countOddEven(j) + arrayCount.countOddEven(k));
						threeD.setPrimeOrComposite(arrayCount.countPrimeOrComposite(i) + arrayCount.countPrimeOrComposite(j) + arrayCount.countPrimeOrComposite(k));
						threeD.setOrder(arrayCount.countOrder(threeD));
						threeD.setInterval(arrayCount.countInterval(threeD));
						threelist.add(threeD);
					}
				}
			}
			log.info(threelist.get(0).toString());
			for(int i=0;i<1000;i++) {
				arrayThreeMapper.insert1000(threelist.get(i));
			}
			
			 	long endTime=System.currentTimeMillis();
		        log.info("执行时间："+(endTime-startTime)+"ms");
				sqlSession.commit();//提交数据库操作
			} catch(Exception e) {
			    e.printStackTrace();
			}
			finally {
				if (sqlSession != null) {
					sqlSession.close();
				}
			}
		
		
	}
	
	/**
	 **  计算除三余数
	 **
	 **/
	@Test
	public static void countRemainder(){
		
		long startTime=System.currentTimeMillis();
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		log.info(dateFormat.format(date));
		//创建HSSFWorkbook对象  
	   	HSSFWorkbook wb = new HSSFWorkbook(); 
	   	SXSSFWorkbook sxssbook=new SXSSFWorkbook();
		
		 String number="1000";
		 String fileURL="E:/lottery/1000.xls";
		 BufferedOutputStream bos = null;
		 try {
				
				//按和值分类开奖数据
				List<ThreeD> threelist= new ArrayList<ThreeD>();
				
				ArrayCount arrayCount=new ArrayCount();
				for(int i=0;i<=9;i++) {
					for(int j=0;j<=9;j++) {
						for(int k=0;k<=9;k++) {
							ThreeD threeD=new ThreeD();
							log.info(i+""+j+""+k);
							threeD.setHundreds(i);
							threeD.setTens(j);
							threeD.setOnes(k);
							threeD.setResult(i+""+j+""+k);
							threeD.setForm(	arrayCount.countForm(threeD));
							threeD.setSpan(arrayCount.countSpan(threeD));
							threeD.setSum(arrayCount.countSum(threeD));
							threeD.setPacket(arrayCount.countPacket(threeD));
							threeD.setRemainder(arrayCount.countRemainder(threeD));
							threeD.setSize(arrayCount.countSize(i) + arrayCount.countSize(j) + arrayCount.countSize(k));
							threeD.setOddeven(arrayCount.countOddEven(i) + arrayCount.countOddEven(j) + arrayCount.countOddEven(k));
							threeD.setPrimeOrComposite(arrayCount.countPrimeOrComposite(i) + arrayCount.countPrimeOrComposite(j) + arrayCount.countPrimeOrComposite(k));
							threeD.setOrder(arrayCount.countOrder(threeD));
							threeD.setInterval(arrayCount.countInterval(threeD));
							threelist.add(threeD);
						}
					}
				}
				
				
			
				 for(int row=0;row<threelist.size();row++) {
					 log.info(threelist.get(row).toString());
				 }
				
					 
						
						
						//创建HSSFSheet对象  
						HSSFSheet sheet = wb.createSheet(number); 
						//创建行  
						HSSFRow row2=sheet.createRow(0);      
						//创建单元格并设置单元格内容  
						  
						row2.createCell(0).setCellValue("编号");  
						row2.createCell(1).setCellValue("百位");  
						row2.createCell(2).setCellValue("十位");  
						row2.createCell(3).setCellValue("个位");      
						row2.createCell(4).setCellValue("形态"); 
						row2.createCell(5).setCellValue("跨度");  
						row2.createCell(6).setCellValue("和值");
						row2.createCell(7).setCellValue("组选");
						row2.createCell(8).setCellValue("除三余数");
						row2.createCell(9).setCellValue("大小");
						row2.createCell(10).setCellValue("奇偶");
						row2.createCell(11).setCellValue("质合");
						row2.createCell(12).setCellValue("升降");
						row2.createCell(13).setCellValue("区间");
						  
						
						 
						
					   	CellStyle cellStyle = wb.createCellStyle();
					   	cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
					   	cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
				         
						HSSFFont font = wb.createFont();
						font.setFontName("Arial");
						font.setFontHeightInPoints((short) 12);
						cellStyle.setFont(font);
						
						
						//表头设置垂直居中、水平居中
						for(int j=0;j<=13;j++) {
							 row2.getCell(j).setCellStyle(cellStyle);
					    }
						 log.info("threelist.size():"+threelist.size());
					    for(int row=0;row<threelist.size();row++) {
					    	 HSSFRow row3=sheet.createRow(row+1);      
					 	    //创建单元格并设置单元格内容  
					 	    row3.createCell(0).setCellValue(row);  
					 	    row3.createCell(1).setCellValue(threelist.get(row).getHundreds());  
					 	    row3.createCell(2).setCellValue(threelist.get(row).getTens());      
					 	    row3.createCell(3).setCellValue(threelist.get(row).getOnes());  
					 	    row3.createCell(4).setCellValue(threelist.get(row).getForm());  
					 	    row3.createCell(5).setCellValue(threelist.get(row).getSpan());  
					 	    row3.createCell(6).setCellValue(threelist.get(row).getSum());      
					 	    row3.createCell(7).setCellValue(threelist.get(row).getPacket()); 
					 	    row3.createCell(8).setCellValue(threelist.get(row).getRemainder()); 
					 	    row3.createCell(9).setCellValue(threelist.get(row).getSize()); 
					 	    row3.createCell(10).setCellValue(threelist.get(row).getOddeven()); 
					 	    row3.createCell(11).setCellValue(threelist.get(row).getPrimeOrComposite()); 
					 	    row3.createCell(12).setCellValue(threelist.get(row).getOrder()); 
					 	    row3.createCell(13).setCellValue(threelist.get(row).getInterval()); 
					 	 
					 	    //设置单元格垂直居中、水平居中
					 	    for(int j=0;j<=13;j++) {
					 		  row3.getCell(j).setCellStyle(cellStyle);
						    }
					 	    
					 	    
					 	  
					    }
					    

					    
					   
					  
					    // 必须在单元格设值以后进行
					    // 设置为根据内容自动调整列宽 中文不适用
				        for (int k = 0; k < 8; k++) {
				            sheet.autoSizeColumn(k);
				        }   
				        log.info("Sheet"+number+"分类完毕！");
				
				
				//输出Excel文件  
		        //文件路径（路径+文件名）
				File file = new File(fileURL);
			   	//文件不存在则创建文件，先创建目录
			    if (!file.exists()) {   
			        File dir = new File(file.getParent());
			        dir.mkdirs();
			        file.createNewFile();
			    }
			   	FileOutputStream output=new FileOutputStream(fileURL); 
			   	bos = new BufferedOutputStream(output);
			   	
			   	wb.write(bos); 
			   	
			   	wb.close();
			   	output.flush(); 
			   	
	            long endTime=System.currentTimeMillis();
		        log.info("xls执行时间："+(endTime-startTime)+"ms");
				
			     
			 } catch (Exception e) {
			     e.printStackTrace();
			     // do something
			 } finally {
			       // do something
				
				 
				 if(bos != null){
		                try {
		                    bos.close();
		                } catch (IOException e) {
		                    e.printStackTrace();
		                }

		            }
			 }
				  
				  
			}
}
